package com.mfin.bom.adapter;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.mfin.bom.interfaces.ICustomer;
import com.mfin.bom.interfaces.IFinance;
import com.mfin.bom.interfaces.IFinanceLoan;
import com.mfin.bom.interfaces.ILoanMaster;
import com.mfin.bom.interfaces.IPartner;
import com.mfin.bom.objects.Customer;
import com.mfin.bom.objects.FinanceLoan;
import com.mfin.enumeration.LoanStatus;
import com.mfin.vo.ObjectKey;

public class FinanceLoanAdapter extends IIdentifiableAdapter implements
		IFinanceLoan {

	/**
	 * 
	 */
	private static final long serialVersionUID = 185952867125739337L;

	FinanceLoan wrappedFinanceLoan;

	public FinanceLoanAdapter(FinanceLoan loanApplication, ObjectKey[] keys) {
		wrappedFinanceLoan = loanApplication;
		ObjectKey[] newKeys = new ObjectKey[3];
		newKeys[0] = new ObjectKey(keys[0].getId());
		newKeys[1] = new ObjectKey(keys[1].getId());
		newKeys[2] = new ObjectKey(wrappedFinanceLoan.getId());
		setKeys(newKeys);
	}

	public FinanceLoan getFinanceLoan() {
		return wrappedFinanceLoan;
	}

	public FinanceLoan getLoanApplication() {
		return wrappedFinanceLoan;
	}

	@Override
	public Long getLoanId() {
		return wrappedFinanceLoan.getId();
	}

	@Override
	public void setLoanId(Long id) {
		wrappedFinanceLoan.setId(id);
	}

	@Override
	public ICustomer getCustomer() {
		return new CustomerAdapter(wrappedFinanceLoan.getCustomer(), getKeys());
	}

	@Override
	public void setCustomer(ICustomer bomCustomer) {
		if (bomCustomer != null) {
			CustomerAdapter customerAdapter = (CustomerAdapter) bomCustomer;
			wrappedFinanceLoan.setCustomer(customerAdapter.getCustomer());
		}
	}

	@Override
	public IFinance getFinance() {
		return new FinanceAdapter(wrappedFinanceLoan.getFinance());
	}

	@Override
	public void setFinance(IFinance bomFinanceInformation) {
		if (bomFinanceInformation != null) {
			FinanceAdapter financeInformationAdapter = (FinanceAdapter) bomFinanceInformation;
			wrappedFinanceLoan.setFinance(financeInformationAdapter
					.getFinance());
		}
	}

	@Override
	public IPartner getPartner() {
		return new PartnerAdapter(wrappedFinanceLoan.getPartner(), getKeys());
	}

	@Override
	public void setPartner(IPartner bomPartner) {
		if (bomPartner != null) {
			PartnerAdapter partnerAdapter = (PartnerAdapter) bomPartner;
			wrappedFinanceLoan.setPartner(partnerAdapter.getPartner());
		}
	}

	@Override
	public ILoanMaster getLoanmaster() {
		return new LoanMasterAdapter(wrappedFinanceLoan.getLoanMaster());
	}

	@Override
	public void setLoanMaster(ILoanMaster bomLoanmaster) {
		if (bomLoanmaster != null) {
			LoanMasterAdapter loanmasterAdapter = (LoanMasterAdapter) bomLoanmaster;
			wrappedFinanceLoan.setLoanMaster(loanmasterAdapter.getLoanMaster());
		}
	}

	@Override
	public Date getLoanDate() {
		return wrappedFinanceLoan.getLoanDate();
	}

	@Override
	public void setLoanDate(Date loanDate) {
		wrappedFinanceLoan.setLoanDate(loanDate);
	}

	@Override
	public Double getLoanAmount() {
		return wrappedFinanceLoan.getLoanAmount();
	}

	@Override
	public void setLoanAmount(Double loanAmount) {
		wrappedFinanceLoan.setLoanAmount(loanAmount);
	}

	@Override
	public Integer getDuration() {
		return wrappedFinanceLoan.getDuration();
	}

	@Override
	public void setDuration(Integer duration) {
		wrappedFinanceLoan.setDuration(duration);
	}

	@Override
	public Double getInterest() {
		return wrappedFinanceLoan.getInterest();
	}

	@Override
	public void setInterest(Double interest) {
		wrappedFinanceLoan.setInterest(interest);
	}

	@Override
	public Double getDocCharges() {
		return wrappedFinanceLoan.getDocCharges();
	}

	@Override
	public void setDocCharges(Double docCharges) {
		wrappedFinanceLoan.setDocCharges(docCharges);
	}

	@Override
	public Date getDuedate() {
		return wrappedFinanceLoan.getDuedate();
	}

	@Override
	public void setDuedate(Date duedate) {
		wrappedFinanceLoan.setDuedate(duedate);
	}

	@Override
	public Double getInstallment() {
		return wrappedFinanceLoan.getInstallment();
	}

	@Override
	public void setInstallment(Double installment) {
		wrappedFinanceLoan.setInstallment(installment);
	}

	@Override
	public Boolean getSecurity() {
		return wrappedFinanceLoan.getSecurity();
	}

	@Override
	public void setSecurity(Boolean security) {
		wrappedFinanceLoan.setSecurity(security);
	}

	@Override
	public String getSecDetails() {
		return wrappedFinanceLoan.getSecDetails();
	}

	@Override
	public void setSecDetails(String secDetails) {
		wrappedFinanceLoan.setSecDetails(secDetails);
	}

	@Override
	public LoanStatus getLoanStatus() {
		return wrappedFinanceLoan.getLoanStatus();
	}

	@Override
	public void setLoanStatus(LoanStatus loanStatus) {
		wrappedFinanceLoan.setLoanStatus(loanStatus);
	}

	@Override
	public Set<ICustomer> getGuarantors() {
		Set<Customer> guarantorSet = wrappedFinanceLoan.getGuarantors();
		Set<ICustomer> bomCustomerSet = new HashSet<ICustomer>();

		if (guarantorSet != null) {
			for (Customer guarantor : guarantorSet) {
				bomCustomerSet.add(new CustomerAdapter(guarantor, getKeys()));
			}
		}
		return bomCustomerSet;
	}

	@Override
	public void setGuarantors(Set<ICustomer> guarantors) {

		if (guarantors != null) {
			Set<Customer> guarantorSet = new HashSet<Customer>();

			for (ICustomer bomGaurantor : guarantors) {
				guarantorSet
						.add(((CustomerAdapter) bomGaurantor).getCustomer());
			}
			wrappedFinanceLoan.setGuarantors(guarantorSet);
		}
	}
	/*
	 * @OneToMany(fetch = FetchType.LAZY, mappedBy = "loanApplication") public
	 * Set<Cashbook> getCashbooks() { return this.cashbooks; }
	 * 
	 * public void setCashbooks(Set<Cashbook> cashbooks) { this.cashbooks =
	 * cashbooks; }
	 */
}
